<?php
/**
 * Check if exist Courses in database, return true if exist Courses code else return false
 * 
 * @param string $seitcm_courses_code Courses NameCode to check
 * 
 * @return bool 
 */
function seitcm_check_exist_courses_code($seitcm_courses_code){
	global $wpdb;
	$wpdb->get_results($wpdb->prepare('SELECT id FROM ' . SEITCM_TABLE_COURSES . ' WHERE codename = %s',$seitcm_courses_code));
	if($wpdb->num_rows == 0) return false;
	return true;
}
/**
 * Check if exist Courses in database, return true if exist ID else return false
 *
 * @param int $id Courses ID to check
 *
 * @return bool
 */
function seitcm_check_exist_courses($id){
	global $wpdb;
	$wpdb->get_results($wpdb->prepare('SELECT id FROM ' . SEITCM_TABLE_COURSES . ' WHERE id = %s',$id));
	if($wpdb->num_rows == 0) return false;
	return true;
}
/**
 * 
 * @param string $seitcm_courses_code
 * @param string $seitcm_courses_name
 * @param string $seitcm_courses_note
 * @param number $seitcm_fees_cost
 * @param string $seitcm_fees_apply_since
 * @return boolean
 */
function seitcm_add_courses_include_fees($seitcm_courses_code,$seitcm_courses_name,$seitcm_courses_note,$seitcm_fees_cost,$seitcm_fees_apply_since){
	global $wpdb;
	$wpdb->query('start transaction');
	$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_COURSES . ' (codename,name,note) VALUES (%s,%s,%s)',$seitcm_courses_code,$seitcm_courses_name,$seitcm_courses_note);
	if(!($wpdb->query($sql)))
	{
		$wpdb->query('rollback');
		return false;
	}
	$seitcm_current_courses_id = seitcm_get_single_courses_bycodename($seitcm_courses_code)->id;
	$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_FEES . ' (coursesid,cost,applysince) VALUES (%d,%d,%s)',$seitcm_current_courses_id,$seitcm_fees_cost,$seitcm_fees_apply_since);
	if(!($wpdb->query($sql)))
	{
		$wpdb->query('rollback');
		return false;
	}
	$wpdb->query('commit');
	return true;
}

function seitcm_update_courses_include_fees($seitcm_courses_code,$seitcm_courses_name,$seitcm_courses_note,$seitcm_courses_id,$seitcm_fees_cost,$seitcm_fees_apply_since){
	global $wpdb;
	$wpdb->query('start transaction');
	$sql = $wpdb->prepare('UPDATE ' . SEITCM_TABLE_COURSES . ' SET codename = %s, name = %s, note = %s WHERE id = %d',
			$seitcm_courses_code,$seitcm_courses_name,$seitcm_courses_note,$seitcm_courses_id);
	try {
		$wpdb->query($sql);
	}
	catch (Exception $e)
	{
		echo $sql;
		$wpdb->query('rollback');
		return false;
	}
	$seitcm_current_fee = seitcm_get_courses_current_fee($seitcm_courses_id);
	if($seitcm_current_fee == null || $seitcm_current_fee->cost != $seitcm_fees_cost)
	{
		$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_FEES . ' (coursesid,cost,applysince) VALUES (%d,%d,%s)',$seitcm_courses_id,$seitcm_fees_cost,$seitcm_fees_apply_since);
		if(!($wpdb->query($sql)))
		{
			$wpdb->query('rollback');
			return false;
		}
	}
	$wpdb->query('commit');
	return true;
}
/**
 * Get Courses List
 * @param number $limit
 * @param number $offset
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >|NULL
 */
function seitcm_get_list_courses($limit=10,$offset=0){
	global $wpdb;
	try{
		return $wpdb->get_results($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_COURSES . ' ORDER BY id DESC LIMIT %d OFFSET %d',$limit,$offset));
	}
	catch (Exception $e) {
		return null;
	}
}
/**
 * Get Courses Count
 *
 * @return integer
 */
function seitcm_get_count_courses(){
	global $wpdb;
	try{
		return $wpdb->get_var('SELECT count(id) FROM '. SEITCM_TABLE_COURSES);
	}
	catch (Exception $e) {
		return -1;
	}
}
/**
 * Get Simple Courses
 *
 * @param int $id id of course want to get
 *
 * @return Ambigous <mixed, NULL, multitype:>|NULL
 */
function seitcm_get_single_courses($id){
	global $wpdb;
	try{
		return $wpdb->get_row($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_COURSES . ' WHERE id=%d',$id));
	}
	catch (Exception $e) {
		return null;
	}
}
/**
 * Get Simple Courses by codename
 *
 * @param string $codename codename of course want to get
 *
* @return Ambigous <mixed, NULL, multitype:>|NULL
 */
function seitcm_get_single_courses_bycodename($codename){
	global $wpdb;
	try{
		return $wpdb->get_row($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_COURSES . ' WHERE codename=%s',$codename));
	}
	catch (Exception $e) {
		return null;
	}
}

/**
 * Get curent fees for courses
 * 
 * @param integer $id
 * 
 * @return Ambigous <mixed, NULL, multitype:>|NULL
 */
function seitcm_get_courses_current_fee($coursesid){
	global $wpdb;
	try {
		return  $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . SEITCM_TABLE_FEES . ' WHERE `coursesid` = %s ORDER BY `applysince` DESC',$coursesid));
	}
	catch (Exception $e){
		return null;
	}
}
/**
 * Get Fees List for a courses
 * 
 * @param unknown $id
 * 
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >|NULL
 */
function seitcm_get_courses_fees_history($coursesid){
	global $wpdb;
	try{
		return $wpdb->get_results($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_FEES.' WHERE coursesid = %d ORDER BY `applysince` DESC',$coursesid));
	}
	catch (Exception $e) {
		return null;
	}
}

/**
 * Get Fees Count for a courses
 * 
 * @param unknown $id
 * 
 * @return Ambigous <string, NULL>|number
 */
function seitcm_get_courses_fees_history_count($coursesid){
	global $wpdb;
	try{
		return $wpdb->get_var($wpdb->prepare('SELECT count(id) FROM '. SEITCM_TABLE_FEES.' WHERE coursesid = %d',$coursesid));
	}
	catch (Exception $e) {
		return -1;
	}
}
/**
 * Get Fees List
 *
 * @return fees object array
 */
function seitcm_get_list_fees(){
	global $wpdb;
	try{
		return $wpdb->get_results('SELECT * FROM '. SEITCM_TABLE_FEES);
	}
	catch (Exception $e) {
		return null;
	}
}

/**
 * Get Fees Count
 *
 * @return integer
 */
function seitcm_get_count_fees(){
	global $wpdb;
	try{
		return $wpdb->get_var('SELECT count(id) FROM '. SEITCM_TABLE_FEES);
	}
	catch (Exception $e) {
		return -1;
	}
}

/**
 * Get Simple Fees
 *
 * @param int $id id of fees want to get
 *
 * @return Ambigous <mixed, NULL, multitype:>|NULL (fees object)
 */
function seitcm_get_single_fees($id){
	global $wpdb;
	try{
		return $wpdb->get_row($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_FEES . ' WHERE id=%d',$id));
	}
	catch (Exception $e) {
		return null;
	}
}
/**
 * Update a Fees, return true if success, false if error
 *
 * @param string $seitcm_fees_id Fess ID
 * @param string $seitcm_fees_courses_id Courses ID
 * @param string $seitcm_fees_cost How much money for courses
 * @param string $seitcm_fees_applysince Time to apply this fees
 *
 * @return bool
 */

?>